![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
create-nestjs-middleware-module
Advanced tools
NestJS configured middleware module made simple
It is a tiny helper library that helps you create simple idiomatic NestJS module based on Express
/Fastify
middleware in just a few lines of code with routing out of the box.
npm i create-nestjs-middleware-module
or
yarn add create-nestjs-middleware-module
Let's imaging you have some middleware factory, for example, simple logger:
export interface Options {
maxDuration: number
}
export function createResponseDurationLoggerMiddleware(opts: Options) {
return (request, response, next) => {
const start = Date.now();
response.on('finish', () => {
const message = `${request.method} ${request.path} - ${duration}ms`;
const duration = Date.now() - start;
if (duration > maxDuration) {
console.warn(message);
} else {
console.log(message);
}
});
next();
};
}
And you want to create an idiomatic NestJS module based on that middleware. Just pass this middleware factory to createModule
function:
import { createModule } from 'create-nestjs-middleware-module';
import { Options, createResponseDurationLoggerMiddleware } from './middleware';
export const TimingModule = createModule<Options>(createResponseDurationLoggerMiddleware);
That's it, your module is ready. Let's see what API it has:
import { TimingModule } from './timing-module';
import { MyController } from './my.controller';
@Module({
imports: [
// 1. `.forRoot()` method accept params satisfying `Options` interface
TimingModule.forRoot({ maxDuration: 1000 }),
// 2. `.forRoot()` method accept additional optional routing params
TimingModule.forRoot({
maxDuration: 1000,
// both `forRoutes` and `exclude` properties are optional
// and has the same API as NestJS buil-in `MiddlewareConfigProxy`
// @see https://docs.nestjs.com/middleware#applying-middleware
forRoutes: [MyController],
exclude: [{ method: RequestMethod.ALL, path: 'always-fast' }],
}),
// 3. `.forRootAsync()` method with only factory
TimingModule.forRootAsync({
useFactory: async () => {
return { maxDuration: 1000 }
}
}),
// 4. `.forRootAsync()` method with dependencies
TimingModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
return { maxDuration: config.maxDurationForAPIHandler }
}
}),
// 5. `.forRootAsync()` method with routing
TimingModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
return {
maxDuration: config.maxDurationForAPIHandler
// both `forRoutes` and `exclude` properties are optional
// and has the same API as NestJS buil-in `MiddlewareConfigProxy`
// @see https://docs.nestjs.com/middleware#applying-middleware
forRoutes: [MyController],
exclude: [{ method: RequestMethod.ALL, path: 'always-fast' }],
};
}
}),
]
controllers: [MyController /*, ... */]
})
class App {}
See examples of usage in __tests__
folder or nestjs-session and nestjs-cookie-session packages
createModule
callback function can return not only one middleware, but array of it.import { createModule } from 'create-nestjs-middleware-module';
interface Options {
// ...
}
createModule<Options>((options) => {
function firstMidlleware() { /* ... */ }
function secondMidlleware() { /* ... */ }
return [firstMidlleware, secondMidlleware]
});
Options
interface has not required properties it can be frustrating to force end-users of your module to call forRoot({})
, and for better developer expirience you can cast createModule(...)
result to FacadeModuleStaticOptional<Options>
, then forRoot()
could be called without arguments and without TS error. In such case createModule
callback function will be called with empty object {}
.import { createModule, FacadeModuleStaticOptional } from 'create-nestjs-middleware-module';
interface Options {
maxDuration?: number;
}
createModule<Options>((options) => {
typeof options // always "object" even if not passed to `forRoot()`
return (request, response, next) => {
// ...
next();
};
}) as FacadeModuleStaticOptional<Options>;
forRoot
and forRootAsync
argument:import {
AsyncOptions,
SyncOptions,
} from 'create-nestjs-middleware-module';
interface Options {
// ...
}
export type MyModuleOptions = SyncOptions<Options>;
export type MyModuleAsyncOptions = AsyncOptions<Options>;
express
and fastify
. But you should be aware that middlewares of express
are not always work with fastify
and vise versa. Sometimes you can check platforms internally. Sometimes maybe it's better to create 2 separate modules for each platform. It's up to you.FAQs
NestJS configured middleware module made simple
The npm package create-nestjs-middleware-module receives a total of 7,160 weekly downloads. As such, create-nestjs-middleware-module popularity was classified as popular.
We found that create-nestjs-middleware-module demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.